<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Article extends Model
{
    protected $fillable = [
        'file','title','url','image','style','cat_id1','cat_id2','viewnum','downnum','collecnum','status','is_del','note','type','content'
    ];
    public function catid1(){
        return $this->hasOne('App\Cat','id','cat_id1');
    }
    public function catid2(){
        return $this->hasOne('App\Cat','id','cat_id2');
    }
    public function cstyle(){
        return $this->hasOne('App\Cat','id','style');
    }

    /**
     * 获得此用户收藏的所有模型。
     */
    public function article()
    {
        return $this->morphMany('App\Collect', 'commentable');
    }

    //案例封面获取
    public function getImageAttribute($value)
    {
        if ((strpos($value, 'http') > -1)) {
            $avatar = $value;
        } else {
            $avatar = env('APP_IMGURL') . '/avatar/' . $value;
        }
        return $avatar;
    }

    //案例封面获取
    public function getUrlAttribute($value)
    {
        $urls = explode('/',$value);
        return explode('.',end($urls))[0];
    }
}
